using System;
using System.Collections.Generic;
using System.Text;
using TALoaders;

namespace TALoaders.Tests
{
    using NUnit.Framework;

    [TestFixture]
    class TestOandaFXLoader
    {
        private OandaFXLoader fxLoaderM = new OandaFXLoader();

        [Test]
        [Category("DataCollection")]
        public void getLoaderType()
        {
            Assert.AreEqual(fxLoaderM.Loader, Loader.OandaFX, "Loader Type");
        }

        [Test]
        [Category("DataCollection")]
        public void getHistoricalData()
        {
            // Test Close
            string ticker = "EURSEK";
            SortedList<DateTime, double> close = fxLoaderM.getHistoricalPrices(ticker, new DateTime(2010, 1, 5), new DateTime(2010, 2, 5), QuoteType.Close);
            Assert.AreEqual(10.2066d, close.Values[2], "Testing Close");

            double totalClose = 0;
            foreach (double dailyClose in close.Values)
                totalClose += dailyClose;
            Assert.AreEqual(326.2607d, totalClose, "Testing Total Close");
        }

        [Test]
        [Category("DataCollection")]
        public void getOpenHighVolumeNotImplemented()
        {
            string ticker = "EURUSD";
            DateTime startDate = new DateTime(2007, 01, 01);
            DateTime endDate = new DateTime(2010, 1, 1);
            Assert.Throws<System.Exception>(
                delegate { fxLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.High); },
                "Oanda does not support HIGH");
            Assert.Throws<System.Exception>(
                delegate { fxLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.Low); },
                "Oanda does not support LOW");
            Assert.Throws<System.Exception>(
                delegate { fxLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.Volume); },
                "Oanda does not support VOLUME");
            Assert.Throws<System.Exception>(
                delegate { fxLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.Open); },
                "Oanda does not support OPEN");
        }

        [Test]
        [Category("DataCollection")]
        public void getLargeHistoricalData()
        {
            string ticker = "EURUSD";
            DateTime startDate = new DateTime(2007, 01, 01);
            DateTime endDate = new DateTime(2010, 1, 1);
            SortedList<DateTime, double> close = fxLoaderM.getHistoricalPrices(ticker, startDate, endDate, QuoteType.Close);
            Assert.AreEqual(1097, close.Count, "# of historical data points");
            Assert.AreEqual(1.3203, close.Values[0], "EURUSD in 2007-01-01");
            Assert.AreEqual(1.4369000000000001d, close.Values[close.Count - 1], "EURUSD on 2010-1-1");
        }

        [Test]
        [Category("DataCollection")]
        public void getLastPrice()
        {
            string ticker = "EURUSD";
            DateTime start = DateTime.Now;
            start = start.AddDays(-5);

            SortedList<DateTime, double> history = fxLoaderM.getHistoricalPrices(ticker, start, DateTime.Now, QuoteType.Close);
            double lastHistory = history.Values[history.Count - 1];
            string timeStamp;
            double lastPrice = fxLoaderM.getLastPrice(ticker, out timeStamp);
            Assert.Less(Math.Abs(lastHistory - lastPrice) / lastPrice, 0.05); // Assume that moves less than 5% in a day
        }
    }
}
